package it.intecs.pisa.smarcos.activities;

import it.intecs.pisa.smarcos.R;
import it.intecs.pisa.smarcos.base.BeepManager;
import it.intecs.pisa.smarcos.base.ConfigurationManager;
import it.intecs.pisa.smarcos.base.GestureEvents;
import it.intecs.pisa.smarcos.base.GestureIntents;
import it.intecs.pisa.smarcos.base.Lists;
import it.intecs.pisa.smarcos.base.SpeechManager;
import it.intecs.pisa.smarcos.util.MathUtils;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;


public class ConfigurationVibration extends BaseActivity {

	public static final String INTENT_SHOW="it.intecs.pisa.smarcos.activities.ConfigurationVibration.SHOW";
	private static final String TAG = ConfigurationVibration.class.getSimpleName();
	
	private SpeechManager speechManager;
	private BeepManager beepManager;
	private ConfigurationManager configurationManager;

	// Common to all features
	View view; 
	Lists list;
	private Vibrator vibrator;
	private ViewGroup baseCenterView;

	private String position;
	LayoutInflater inflater;

	float startX,startY;

	private String text;
	private int counter = 0;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Log.d(TAG, "OnCreate");

		speechManager = SpeechManager.getInstance(getApplicationContext());
		configurationManager = ConfigurationManager.getInstance(getApplicationContext());
		beepManager = BeepManager.getInstance(getApplicationContext());
		vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);

		// Metodo di BaseViewManager
		Init(getBaseContext());
		inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
		view = inflater.inflate(R.layout.generic_option, null);
		setContentView(getBaseView());
		baseCenterView = (ViewGroup) findViewById(R.id.center_base_view);
		baseCenterView.addView(view);

		position = getString(R.string.TTS_Config_Vibration);
		text = getString(R.string.TTS_No);
	
		list = new Lists(3, beepManager);
	}


	@Override
	public void onResume() {
		super.onResume();

		Log.d(TAG, "onResume");
		
		passBy = false;
		speechManager.say(R.string.TTS_Config_Vibration_Swipe);
	}

	@Override
	public void onPause() {
		super.onPause();
		Log.d(TAG, "OnPause");
	}

	// CLOSE
	@Override
	public void onClick_A(){
		Log.d(TAG, "onClick_A");

		Intent returnIntent;
		returnIntent=new Intent("it.intecs.pisa.smarcos.services.ResultReceiver.SHOW");
		returnIntent.putExtra("Result", "NORESULT");
		returnIntent.addCategory(Intent.CATEGORY_DEFAULT);					
		sendBroadcast(returnIntent);
		sentResult = true;
		vibrator.vibrate(300);
		finish();
	}

	// POSITION
	@Override
	public void onClick_B(){
		Log.d(TAG, "onClick_B");
		if(position != null)
			speechManager.say(position, false, false);	
	}

	// REPEAT
	@Override
	public void onClick_C(){
		Log.d(TAG, "onClick_C");
		speechManager.say(text, false, false);	
	}

	// DONE -> as CLOSE 
	@Override
	public void onClick_D(){
		Log.d(TAG, "onClick_D");
		onClick_A();
	}


	@Override
	public void onClick_Center(int event, float X, float Y){
		Log.d(TAG, "onClick_Center");

		float lenghtTreshold=10;

		switch(event)
		{
		case GestureEvents.ONFLING_START:

			startX = X;
			startY = Y;
			break;

		case GestureEvents.ONFLING_STOP:

			float endX,endY;

			endX = X;
			endY = Y;

			float gestureLenght=0;

			gestureLenght=MathUtils.distanceBetweenPoints(endX,startX,endY,startY);

			if(gestureLenght>lenghtTreshold){

				String gesture = MathUtils.calculateSlice(endX,startX,endY,startY);
				if (gesture.length() != 0)
					VibrGestureHandling(gesture);
			}
			break;

		case GestureEvents.ONDOUBLETAP:
			vibrator.vibrate(300);
			VibrGestureHandling(GestureIntents.INTENT_GESTURE_SELECT);
			break;
			
		case GestureEvents.ONSCROLL:
			if (passBy == true)
				speechManager.say(position, false, false);
			break;
		}
	}



	private void VibrGestureHandling(String intAction) 
	{
		TextView textView=(TextView) findViewById(R.id.Option);
	

		if (intAction.equals(GestureIntents.INTENT_GESTURE_SELECT)) {

			if ((counter = list.Movement(intAction)) == -1)
				return;
			
			configurationManager.setVibration(text);
			
			if (counter == 0)
				speechManager.say(getString(R.string.TTS_Config_Vibration_No), false, false);
			else if (counter == 1)
				speechManager.say(getString(R.string.TTS_Config_Vibration_Level1), false, false);		
			else
				speechManager.say(getString(R.string.TTS_Config_Vibration_Level2), false, false);
		}
		else {
			if (intAction.equals(GestureIntents.INTENT_GESTURE_RIGHT) ||
					intAction.equals(GestureIntents.INTENT_GESTURE_LEFT)){

				if ((counter = list.Movement(intAction)) == -1)
					return;

				if (counter == 0)
					text = getString(R.string.TTS_No);
				else if (counter == 1)
					text = getString(R.string.TTS_Level1);
				else
					text = getString(R.string.TTS_Level2);

				speechManager.say(text, false, false);
				textView.setText(text);
			} 
		}
	}
}


